from locust import HttpLocust, TaskSet, task
import random
from faker import Faker
from locust import HttpUser, task, between
from locust import LoadTestShape
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
fake = Faker(locale='zh_CN')
import json

# def fakertime():
#     return fake.unix_time()
#
#
# def fakername():
#     return fake.company() + str('的订单') + str(random.randint(0, 999))
#
# def random_page():
#     return random.randint(1,1360)

class QuickstartUser(HttpUser):
    wait_time = between(0.1, 0.5)  # 多少秒用户发起1次请求

    def on_start(self):
        pass

    @task(1)
    def api1(self):
        header = {
            'Cookie': 'accountCenterSessionId=.eJw9zk9Lw0AQBfDvsucedrL_exVbEKOXFkkuYWZ2lroNEZpoKeJ3Nyp4fu_3eJ9qeM1qq0Q4aGPBRQg5kydJ5JisY3QRndGWSkiARJy48dwIWZucs1Ea9jqZhgrFYBGdp8KBiw6cIyJkyisvBh1n-QmLCTobTMGLjtkmAEOgNmoSycOMHzIsb0MmtS04zrJR77Nc_k56MCAmmeS1zQSRCbBhwBUP5SLz6d_Mv_3-cLx1tYOuHpe2PozPd1o_7Xfnx8N47mu79JWv7cv9rT3sxq62dt3hE06TjCu-Cqmvb5B2Vz0.FBN3-g.uFaLc6kFPJBFQsOsUlevr306VAM',
            'Content-Type': 'application/json', 'Accept': '*/*', 'Host': 'inbound-uat.tangees.com', 'Accept-Encoding': 'gzip, deflate, br',
            'Connection': 'keep-alive', 'Content-Length': '30'}

        payload = {'pageSize': 10, 'pageNum': 1}
        with self.client.post(url='/jike-web/wxcp/externalcontact/own_query', headers=header, data=json.dumps(payload),
                              catch_response=True) as response:
            if response.status_code == 200:
                # print(response.text)
                response.success()
            else:
                response.failure("No data")
                pass

    # @task(1)
    # def api2(self):
    #     header = {
    #         "Host": "scrm-test.tangees.com",
    #         "Connection": "keep-alive",
    #         "Content-Length": "482",
    #         "Accept": "*/*",
    #         "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36 Edg/89.0.774.63",
    #         "Content-Type": "application/json;charset=UTF-8",
    #         "Origin": "https://scrm-test.tangees.com",
    #         "Sec-Fetch-Site": "same-origin",
    #         "Sec-Fetch-Mode": "cors",
    #         "Sec-Fetch-Dest": "empty",
    #         "Referer": "https://scrm-test.tangees.com/customer-migration/resign-inheritance/group",
    #         "Accept-Encoding": "gzip, deflate, br",
    #         "Accept-Language": "zh-CN,zh;q=0.9",
    #         "Cookie": "accountCenterSessionId=.eJw9j01PwzAQRP-Lzz2sP3Zt9wipeiGgIiBKL5HX3qhtSpCaAFIR_50IJI5PmhnN-1Ldsai1MhRjpBJj8oG8xqxBnLGag4FIOWQbjDMxG01AiAk1Ijmy2dueOKelm0UCIYmG4jJkKhjYYDGlMAiKsdYyWxEDQWe06DxLcU6iTWDVSuVDGkc5L2c-hRceRUo3pQ_p5reusFr36TzJSnX9RabDP75PcvlzINBeknPgNacMzI4XlRKWrek30J7uX9unnWmbzVw3-1N9C9BeH4e7ZnOtq3reb5_hYbvDuroZ9tXLoL5_ANzIU4Q.E6Gviw.KnO5ggzOaANqttG4biN8dSbVaVg",
    #     }
    #     cookie = {
    #         "accountCenterSessionId": ".eJw9j01PwzAQRP-Lzz2sP3Zt9wipeiGgIiBKL5HX3qhtSpCaAFIR_50IJI5PmhnN-1Ldsai1MhRjpBJj8oG8xqxBnLGag4FIOWQbjDMxG01AiAk1Ijmy2dueOKelm0UCIYmG4jJkKhjYYDGlMAiKsdYyWxEDQWe06DxLcU6iTWDVSuVDGkc5L2c-hRceRUo3pQ_p5reusFr36TzJSnX9RabDP75PcvlzINBeknPgNacMzI4XlRKWrek30J7uX9unnWmbzVw3-1N9C9BeH4e7ZnOtq3reb5_hYbvDuroZ9tXLoL5_ANzIU4Q.E6Gviw.KnO5ggzOaANqttG4biN8dSbVaVg"
    #     }
    #     payload = {'pageSize':10,'pageNum':1}
    #     with self.client.post(url='/test/web/inherit/offline/externalcontact/list', headers=header, cookies=cookie,data=json.dumps(payload),
    #                           catch_response=True) as response:
    #         if response.status_code == 200:
    #             # print(response.text)
    #             response.success()
    #         else:
    #             response.failure("No data")
    #             pass
    #
    # @task(1)
    # def api3(self):
    #     header = {
    #         "Host": "scrm-test.tangees.com",
    #         "Connection": "keep-alive",
    #         # "Content-Length": "482",
    #         "Accept": "*/*",
    #         "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36 Edg/89.0.774.63",
    #         # "Content-Type": "application/json;charset=UTF-8",
    #         "Origin": "https://scrm-test.tangees.com",
    #         "Sec-Fetch-Site": "same-origin",
    #         "Sec-Fetch-Mode": "cors",
    #         "Sec-Fetch-Dest": "empty",
    #         "Referer": "https://scrm-test.tangees.com/customer-migration/resign-inheritance/group",
    #         "Accept-Encoding": "gzip, deflate, br",
    #         "Accept-Language": "zh-CN,zh;q=0.9",
    #         "Cookie": "accountCenterSessionId=.eJw9j01PwzAQRP-Lzz2sP3Zt9wipeiGgIiBKL5HX3qhtSpCaAFIR_50IJI5PmhnN-1Ldsai1MhRjpBJj8oG8xqxBnLGag4FIOWQbjDMxG01AiAk1Ijmy2dueOKelm0UCIYmG4jJkKhjYYDGlMAiKsdYyWxEDQWe06DxLcU6iTWDVSuVDGkc5L2c-hRceRUo3pQ_p5reusFr36TzJSnX9RabDP75PcvlzINBeknPgNacMzI4XlRKWrek30J7uX9unnWmbzVw3-1N9C9BeH4e7ZnOtq3reb5_hYbvDuroZ9tXLoL5_ANzIU4Q.E6Gviw.KnO5ggzOaANqttG4biN8dSbVaVg",
    #     }
    #     cookie = {
    #         "accountCenterSessionId": ".eJw9j01PwzAQRP-Lzz2sP3Zt9wipeiGgIiBKL5HX3qhtSpCaAFIR_50IJI5PmhnN-1Ldsai1MhRjpBJj8oG8xqxBnLGag4FIOWQbjDMxG01AiAk1Ijmy2dueOKelm0UCIYmG4jJkKhjYYDGlMAiKsdYyWxEDQWe06DxLcU6iTWDVSuVDGkc5L2c-hRceRUo3pQ_p5reusFr36TzJSnX9RabDP75PcvlzINBeknPgNacMzI4XlRKWrek30J7uX9unnWmbzVw3-1N9C9BeH4e7ZnOtq3reb5_hYbvDuroZ9tXLoL5_ANzIU4Q.E6Gviw.KnO5ggzOaANqttG4biN8dSbVaVg"
    #     }
    #     param = {'pageSize': 10, 'pageNum': 1}
    #     with self.client.get(url='https://scrm-test.tangees.com/test/web/inherit/offline/group/list', headers=header, verify=False, params=param,catch_response=True) as response:
    #         if response.status_code == 200:
    #             if json.loads(response.text)['errMsg'] == 'success！':
    #                 pass
    #             else:
    #                 response.failure("Timeout")
    #         else:
    #             response.failure("No data")

    def on_stop(self):
        pass


# class StagesShape(LoadTestShape):
#     '''
#     在不同的阶段 具有不同的用户数和 产生率的 图形形状
#
#
#     参数解析:
#         stages ：字典列表，每个字典都具有下列这些键值的阶段:
#             duration -- 持续时间，  经过多少秒后，进入到下个阶段
#             users -- 总用户数
#             spawn_rate -- 产生率，即每秒启动/停止的用户数
#             stop -- 可以在特定阶段停止测试的值
#         stop_at_end -- 可以在所有阶段设置运行后停止
#
#     '''
    #
    # stages = [
    #     {"duration": 300, "users": 10, "spawn_rate": 10},
    #     # {"duration": 600, "users": 100, "spawn_rate": 20},
    #     # {"duration": 900, "users": 150, "spawn_rate": 40}
    # ]
    #
    # def tick(self):
    #
    #     run_time = self.get_run_time()
    #
    #     for stage in self.stages:
    #         if run_time < stage['duration']:
    #             tick_data = (stage['users'],stage['spawn_rate'])
    #             return tick_data
    #
    #     return None
class WebsiteUser(HttpUser):
    tasks = [QuickstartUser]
    host = "https://inbound-uat.tangees.com"
    # host = "https://qwx-uat-mobile.tangees.com"
    wait_time = between(1,1)#多少秒用户发起1次请求



if __name__ == "__main__":
    import os

    os.system("locust -f locust_1.py --web-host=127.0.0.1 --master")